
package cn.com.spic.demo.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import cn.com.spic.demo.api.ProgressReportService;
import cn.com.spic.demo.dao.ActionProDAO;
import cn.com.spic.demo.dao.OrganizationDAO;
import cn.com.spic.demo.dao.ProgressReportDAO;
import cn.com.spic.demo.dto.*;
import cn.com.spic.demo.po.*;
import cn.com.spic.demo.service.ProgressService;
import com.yonyou.diwork.exception.BusinessException;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ProgressReportServiceImpl implements ProgressReportService {

    @Autowired
    private ProgressReportDAO progressReportDAO;

    @Autowired
    private ActionProDAO actionProDAO;
    @Autowired
    private OrganizationDAO organizationDAO;
    @Autowired
    private ProgressService progressService;

    public void insertProgress(ProgressReportDTO[] progressReportDTOs) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowYear = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
        String nowMonth = String.valueOf(Calendar.getInstance().get(Calendar.MONTH) + 1);
        if (progressReportDTOs != null && progressReportDTOs.length > 0) {
            ProgressReportPO[] progressReportPOs = new ProgressReportPO[progressReportDTOs.length];
            for (int i = 0; i < progressReportDTOs.length; i++) {
                ProgressReportPO progressReportPO = new ProgressReportPO();
                progressReportDTOs[i].setSubmitYear(nowYear);
                progressReportDTOs[i].setSubmitMonth(nowMonth);
                progressReportDTOs[i].setSubmitDate((new Date().toString()));
                BeanUtils.copyProperties(progressReportDTOs[i], progressReportPO);
                progressReportPOs[i] = progressReportPO;
            }
            progressReportDAO.insertProgress(progressReportPOs);
        } else {
            throw new BusinessException("填报Dto为空");
        }
    }

    @Override
    public List<ProgressReportDTO> getMaxActRateReportdtos(Map<String, String> map) {
        List<ProgressReportPO> progressReportPOS = progressReportDAO.getMaxActRateReportdtos(map);
        List<ProgressReportDTO> progressReportDTOS = new ArrayList<>();
        if (progressReportPOS != null && progressReportPOS.size() > 0) {
            for (ProgressReportPO po : progressReportPOS) {
                ProgressReportDTO progressReportDTO = new ProgressReportDTO();
                BeanUtils.copyProperties(po, progressReportDTO);
                progressReportDTOS.add(progressReportDTO);
            }
        }
        return progressReportDTOS;
    }

    @Override
    public List<ProgressReportDTO> getjdAndShqkList(Map<String, String> map) {
        List<ProgressReportPO> progressReportPOS = progressReportDAO.getjdAndShqkList(map);
        List<ProgressReportDTO> progressReportDTOS = new ArrayList<>();
        if (progressReportPOS != null && progressReportPOS.size() > 0) {
            for (ProgressReportPO po : progressReportPOS) {
                ProgressReportDTO progressReportDTO = new ProgressReportDTO();
                BeanUtils.copyProperties(po, progressReportDTO);
                progressReportDTOS.add(progressReportDTO);
            }
        }
        return progressReportDTOS;
    }

    @Override
    public List<ProgressReportDTO> getActionByDept(Map<String, String> map) {
        List<ProgressReportPO> progressReportPOS = progressReportDAO.getActionByDept(map);
        List<ProgressReportDTO> progressReportDTOS = new ArrayList<>();
        if (progressReportPOS != null && progressReportPOS.size() > 0) {
            for (ProgressReportPO po : progressReportPOS) {
                ProgressReportDTO progressReportDTO = new ProgressReportDTO();
                BeanUtils.copyProperties(po, progressReportDTO);
                progressReportDTOS.add(progressReportDTO);
            }
        }
        return progressReportDTOS;
    }

    @Override
    public List<EchartDTO> groupReportMonth_ljwcjd(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.groupReportMonth_ljwcjd(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> groupReportMonth_dywcjd(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.groupReportMonth_ljwcjd(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> getActionByZx(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.getActionByZx(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> yuqiJdx(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.yuqiJdx(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> yuqiJdx_xiangxi(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.yuqiJdx_xiangxi(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> getjhwclBypingjunf(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.getjhwclBypingjunf(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> getdblypingjunf(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.getdblypingjunf(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> ejdwjz_charts(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.ejdwjz_charts(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> groupcompany_gztd_a(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.groupcompany_gztd_a(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> groupcompany_gztd_b(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.groupcompany_gztd_b(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> groupcompany_gztd_b_a(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.groupcompany_gztd_b_a(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> zxSjwcAndJhwc(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.zxSjwcAndJhwc(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> yuqiJdx_xiangxi_zx(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.yuqiJdx_xiangxi_zx(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> fileManagerList(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.fileManagerList(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public EchartDTO getFileById(Map<String, String> map) {
        //查询上传的附件名称;
        EchartPo echartPo = progressReportDAO.getFileById(map);
        EchartDTO echartDTO = new EchartDTO();

        BeanUtils.copyProperties(echartPo, echartDTO);
        return echartDTO;
    }

    @Override
    public List<EchartDTO> getReportByDept(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.getReportByDept(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> yuqijdx_sbg(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.yuqijdx_sbg(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> benchmarkingDetail(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.benchmarkingDetail(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    //    更新数据库中集团对于二级单位的审核情况;
    @Override
    public Integer UpdateGroupAudit(String Id, String auditId) {
        return progressReportDAO.UpdateGroupAudit(Id, auditId);
    }

    @Override
    public Integer UpdateUnitDef(String Id, String def4) {
        return progressReportDAO.UpdateUnitDef(Id, def4);
    }

    @Override
    public EchartDTO SelectEnclosureIp(String action) {
        EchartPo echartPo = progressReportDAO.SelectEnclosureIp(action);
        EchartDTO echartDTO = new EchartDTO();
        if (echartPo != null) {
            BeanUtils.copyProperties(echartPo, echartDTO);
        }
        return echartDTO;
    }

    @Override
    public Integer AddEnclosureIp(String actionId) {
        return progressReportDAO.AddEnclosureIp(actionId);
    }


    @Override
    public Integer UpdateEnclosureDefault(String actionId) {
        return progressReportDAO.UpdateEnclosureDefault(actionId);
    }


    @Override
    public List<EchartDTO> groupcompany_gztd_b_b(Map<String, String> map) {
        List<EchartPo> echartDTOS = progressReportDAO.groupcompany_gztd_b_b(map);
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<EchartDTO> getNinePro() {
        List<EchartPo> echartDTOS = progressReportDAO.getNinePro();
        List<EchartDTO> echartDTOList = new ArrayList<EchartDTO>();
        if (echartDTOS != null && echartDTOS.size() > 0) {
            for (EchartPo po : echartDTOS) {
                EchartDTO echartDTO = new EchartDTO();
                BeanUtils.copyProperties(po, echartDTO);
                echartDTOList.add(echartDTO);
            }
        }
        return echartDTOList;
    }

    @Override
    public List<ProgressReportDTO> getAllFinishAcion(Map<String, String> map) {
        List<ProgressReportPO> progressReportPOS = progressReportDAO.getAllFinishAcion(map);
        List<ProgressReportDTO> progressReportDTOS = new ArrayList<>();
        if (progressReportPOS != null && progressReportPOS.size() > 0) {
            for (ProgressReportPO po : progressReportPOS) {
                ProgressReportDTO progressReportDTO = new ProgressReportDTO();
                BeanUtils.copyProperties(po, progressReportDTO);
                progressReportDTOS.add(progressReportDTO);
            }
        }
        return progressReportDTOS;
    }

    @Override
    public List<String> getMaxListActionId(Map<String, String> map) {
        return progressReportDAO.getMaxListActionId(map);
    }

    @Override
    public String getSumMaxActRateReportdtos(Map<String, String> map) {
        return progressReportDAO.getSumMaxActRateReportdtos(map);
    }


    @Override
    public List<ProgressReportDTO> getProgressReportdtosNoLimit(Map<String, String> map) {
        List<ProgressReportPO> progressReportPOS = progressReportDAO.getProgressReportdtosNoLimit(map);
        List<ProgressReportDTO> progressReportDTOS = new ArrayList<>();
        if (progressReportPOS != null && progressReportPOS.size() > 0) {
            for (ProgressReportPO po : progressReportPOS) {
                ProgressReportDTO progressReportDTO = new ProgressReportDTO();
                BeanUtils.copyProperties(po, progressReportDTO);
                progressReportDTOS.add(progressReportDTO);
            }
        }
        return progressReportDTOS;
    }

    private void insertProgressReportDtos(ActionProjectPO dto) {
        try {
            String[] spicFirstDeptId = dto.getSpicFirstDeptId().split(",");
            String[] spicSecDeptId = dto.getSpicSecDeptId().split(",");
            boolean firstHaveDept = isHaveGroupDept(spicFirstDeptId);
            boolean secHaveDept = isHaveGroupDept(spicSecDeptId);
            String[] trueDept = null;
            if (firstHaveDept && secHaveDept) {
                trueDept = spicFirstDeptId;
            } else {
                trueDept = (String[]) ArrayUtils.addAll(spicFirstDeptId, spicSecDeptId);
            }
            ProgressReportPO po = new ProgressReportPO();
            List<String> yearAndMonth = getMonthBetween(dto.getStartTime(), dto.getEndTime());
            for (int s = 0; s < yearAndMonth.size(); s++) {
                for (String dept : trueDept) {
                    po.setActionId(dto.getId());
                    po.setTaskDescribe(dto.getTaskDescription());
                    po.setSpicFirstDeptId(dept);
                    po.setSubmitYear(yearAndMonth.get(s).split("-")[0]);
                    po.setSubmitMonth(yearAndMonth.get(s).split("-")[1]);
                    po.setId(dto.getId().concat(yearAndMonth.get(s).split("-")[0]).concat(yearAndMonth.get(s).split("-")[1]).concat(dept));
                    progressService.save(po, true, false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 拆分进度服务 按照部门直接拆分
     *
     * @param dto
     */
    public void insertProgressReportDtos(ActionProejctDTO dto) {
        try {
            String[] spicFirstDeptId = dto.getSpicFirstDeptId().split(",");
            String[] spicSecDeptId = dto.getSpicSecDeptId().split(",");
            boolean firstHaveDept = isHaveGroupDept(spicFirstDeptId);
            boolean secHaveDept = isHaveGroupDept(spicSecDeptId);
            String[] trueDept = null;
            if (firstHaveDept && secHaveDept) {
                trueDept = spicFirstDeptId;
            } else {
                trueDept = (String[]) ArrayUtils.addAll(spicFirstDeptId, spicSecDeptId);
            }
            ProgressReportPO po = new ProgressReportPO();
            List<String> yearAndMonth = getMonthBetween(dto.getStartTime(), dto.getEndTime());
            for (int s = 0; s < yearAndMonth.size(); s++) {
                for (String dept : trueDept) {
                    po.setActionId(dto.getId());
                    po.setTaskDescribe(dto.getTaskDescription());
                    po.setSpicFirstDeptId(dept);
                    po.setSubmitYear(yearAndMonth.get(s).split("-")[0]);
                    po.setSubmitMonth(yearAndMonth.get(s).split("-")[1]);
                    po.setId(dto.getId().concat(yearAndMonth.get(s).split("-")[0]).concat(yearAndMonth.get(s).split("-")[1]).concat(dept));
                    progressService.save(po, true, false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int saveReportProgress(ProgressReportDTO dto) {
        Map<String, String> poParam = new HashMap<>();

        if (dto.getActualComRate() != null) {
            poParam.put("actualComRate", dto.getActualComRate());
        }
        if (dto.getTaskDescribe() != null) {
            poParam.put("taskDescribe", dto.getTaskDescribe());
        }
        if (dto.getTimeOutMessage() != null) {
            poParam.put("timeOutMessage", dto.getTimeOutMessage());
        }
        if (dto.getNowMessage() != null) {
            poParam.put("nowMessage", dto.getNowMessage());
        }
        if (dto.getId() != null) {
            poParam.put("id", dto.getId());
        }
        if (dto.getSubmitYear() != null) {
            poParam.put("submitYear", dto.getSubmitYear());
        }
        if (dto.getSubmitMonth() != null) {
            poParam.put("submitMonth", dto.getSubmitMonth());
        }
        if (dto.getTelephone() != null) {
            poParam.put("telephone", dto.getTelephone());
        }
        if (dto.getDef() != null) {
            poParam.put("def", dto.getDef());
        }
        if (dto.getDef1() != null) {
            poParam.put("def1", dto.getDef1());
        }
        if (dto.getDef4() != null) {
            poParam.put("def4", dto.getDef4());
        }
        if (dto.getTenantId() != null) {
            poParam.put("tenantId", dto.getTenantId());
        }
        if (dto.getSubmitDate() != null) {
            poParam.put("submitDate", dto.getSubmitDate());
        }
        if (dto.getCreatorId() != null) {
            poParam.put("creatorId", dto.getCreatorId());
        }
        return progressReportDAO.saveReportProgress(poParam);
    }

    @Override
    public int saveReportProgressSetNull(ProgressReportDTO dto) {
        return progressReportDAO.saveReportProgressSetNull(dto);
    }

    @Override
    public void saveParentReportProgress(ProgressReportDTO dto) {
        Map<String, String> poParam = new HashMap<>();

        if (dto.getActualComRate() != null) {
            poParam.put("actualComRate", dto.getActualComRate());
        }
        if (dto.getTaskDescribe() != null) {
            poParam.put("taskDescribe", dto.getTaskDescribe());
        }
        if (dto.getTimeOutMessage() != null) {
            poParam.put("timeOutMessage", dto.getTimeOutMessage());
        }
        if (dto.getNowMessage() != null) {
            poParam.put("nowMessage", dto.getNowMessage());
        }
        if (dto.getActionId() != null) {
            poParam.put("actionId", dto.getActionId());
        }
        if (dto.getSubmitYear() != null) {
            poParam.put("submitYear", dto.getSubmitYear());
        }
        if (dto.getSubmitMonth() != null) {
            poParam.put("submitMonth", dto.getSubmitMonth());
        }
        if (dto.getSpicFirstDeptId() != null) {
            poParam.put("spicFirstDeptId", dto.getSpicFirstDeptId());
        }
        poParam.put("submitDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        progressReportDAO.saveParentReportProgress(poParam);
    }

    @Override
    public ProgressReportDTO getProgressReportdtosNoByActionId(String id) {
        ProgressReportPO progressReportPOS = progressReportDAO.getProgressReportdtosNoByActionId(id);
        ProgressReportDTO progressReportDTO = new ProgressReportDTO();
        if (progressReportPOS != null) {
            BeanUtils.copyProperties(progressReportPOS, progressReportDTO);
        }
        return progressReportDTO;
    }

    //自动生成进度填报任务
    @Override
    public String generateProgress() throws Exception {
        String rtnCode = "";
        try {
            MoreActionProjectExample example = new MoreActionProjectExample();
            MoreActionProjectExample.Criteria criteria = example.createCriteria();
            criteria.andLevelEqualTo("2");
//            criteria.andIdEqualTo("175");
            ProgressReportPO truepo = new ProgressReportPO();
            // 获取所有level为2 的行动项  131项
            List<ActionProjectPO> actionProjectPOS = actionProDAO.generateProgress(example);
            boolean spicFirDeptIsAllDept = false;
            boolean spicSecDeptIsAllDept = false;
            for (ActionProjectPO po : actionProjectPOS) {
                String allSpicFirstDeptId = po.getSpicFirstDeptId();
                String allSpicSecDeptId = po.getSpicSecDeptId();
                if (allSpicFirstDeptId != null) {
                    spicFirDeptIsAllDept = allSpicFirstDeptId.contains("org");  //false -- 全是dept
                }
                if (allSpicSecDeptId != null) {
                    spicSecDeptIsAllDept = allSpicSecDeptId.contains("org"); //false -- 全是dept
                }
                List<GenerateProgressVO> generateProgressVOS = new ArrayList<>();
                String[] trueStrings = null;
                //牵头+配合  全都是部门
//            if(!spicFirDeptIsAllDept && !spicSecDeptIsAllDept){
//                trueStrings = allSpicFirstDeptId.split(",");
//                for(int a=0;a<trueStrings.length;a++){
//                    GenerateProgressVO generateProgressVO = new GenerateProgressVO();
//                    generateProgressVO.setOrgId(trueStrings[a]);
//                    generateProgressVO.setStartTime(po.getStartTime());
//                    generateProgressVO.setEndTime(po.getEndTime());
//                    generateProgressVO.setActionId(po.getId());
//                    generateProgressVO.setActionLevelTwo(po.getId());
//                    generateProgressVO.setIdentity("1");
//                    generateProgressVOS.add(generateProgressVO);
//                }
//            }else
                if ((spicSecDeptIsAllDept && !spicFirDeptIsAllDept) || (!spicFirDeptIsAllDept && !spicSecDeptIsAllDept)) {   //牵头部门+配合含有二级单位
                    Map<String, String> map = new HashMap<>();
                    map.put("parentProjectId", po.getId());
                    map.put("level", "3");

                    List<ActionProjectPO> actionPos = actionProDAO.getSpicGroupCode(map);
                    if (allSpicFirstDeptId != null) {
                        trueStrings = allSpicFirstDeptId.split(",");
                    }
                    if (trueStrings != null) {
                        for (String s : trueStrings) {
                            GenerateProgressVO generateProgressVO = new GenerateProgressVO();
                            generateProgressVO.setOrgId(s);
                            generateProgressVO.setStartTime(po.getStartTime());
                            generateProgressVO.setEndTime(po.getEndTime());
                            generateProgressVO.setActionId(po.getId());
                            generateProgressVO.setActionLevelTwo(po.getId());
                            generateProgressVO.setIdentity("1");
                            generateProgressVOS.add(generateProgressVO);
                        }
                    }
                    for (ActionProjectPO po1 : actionPos) {
                        GenerateProgressVO generateProgressVO = new GenerateProgressVO();
                        generateProgressVO.setIdentity("2");
                        generateProgressVO.setOrgId(po1.getSpicGroupId());
                        generateProgressVO.setStartTime(po1.getStartTime());
                        generateProgressVO.setEndTime(po1.getEndTime());
                        generateProgressVO.setActionId(po1.getId());
                        generateProgressVO.setActionLevelTwo(po.getId());
                        generateProgressVOS.add(generateProgressVO);
                    }
                } else {
                    Map<String, String> map = new HashMap<>();
                    map.put("parentProjectId", po.getId());
                    map.put("level", "3");
                    String firstDepts[] = po.getSpicFirstDeptId().split(",");
                    String secDepts[] = po.getSpicSecDeptId().split(",");
                    List<ActionProjectPO> actionPos = actionProDAO.getSpicGroupCode(map);
                    for (ActionProjectPO po1 : actionPos) {
                        GenerateProgressVO generateProgressVO = new GenerateProgressVO();
                        if (Arrays.asList(firstDepts).contains(po1.getSpicGroupId())) {
                            generateProgressVO.setIdentity("1");
                        } else if (Arrays.asList(secDepts).contains(po1.getSpicGroupId())) {
                            generateProgressVO.setIdentity("2");
                        }
                        generateProgressVO.setOrgId(po1.getSpicGroupId());
                        generateProgressVO.setStartTime(po1.getStartTime());
                        generateProgressVO.setEndTime(po1.getEndTime());
                        generateProgressVO.setActionId(po1.getId());
                        generateProgressVO.setActionLevelTwo(po.getId());
                        generateProgressVOS.add(generateProgressVO);
                    }
                }
                for (GenerateProgressVO vo : generateProgressVOS) {

                    String startTime = vo.getStartTime().replace("\n", ""); //行动项开始日期
                    String endTime = vo.getEndTime().replace("\n", "");     //行动项结束日期
                    List<String> yearAndMonth = getMonthBetween(startTime, endTime);  //获取两个日期之间的月份

                    for (int s = 0; s < yearAndMonth.size(); s++) {
                        Map<String, String> map1 = new HashMap<>();
                        map1.put("actionId", vo.getActionId());
                        map1.put("submitYear", yearAndMonth.get(s).split("-")[0]);
                        map1.put("submitMonth", yearAndMonth.get(s).split("-")[1]);
                        map1.put("spicFirstDeptId", vo.getOrgId());
                        List<ProgressReportPO> pos = progressReportDAO.getProgressReportdtosNoLimit(map1);
                        if (pos.size() == 0) {
                            truepo.setActionId(vo.getActionId());
                            truepo.setTaskDescribe(po.getTaskDescription());
                            truepo.setSpicFirstDeptId(vo.getOrgId());
                            truepo.setSubmitYear(yearAndMonth.get(s).split("-")[0]);
                            truepo.setSubmitMonth(yearAndMonth.get(s).split("-")[1]);
                            truepo.setDef2(vo.getActionLevelTwo());
                            truepo.setId(UUID.randomUUID().toString());
                            truepo.setDef3(vo.getIdentity());
                            truepo.setDef4("0");
                            progressService.save(truepo, true, false);
                        } else {
                            Map<String, String> mapAn = new HashMap<>();
                            mapAn.put("def2", pos.get(0).getDef2().concat(",").concat(vo.getActionLevelTwo()));
                            mapAn.put("id", pos.get(0).getId());
                            mapAn.put("submitYear", pos.get(0).getSubmitYear());
                            mapAn.put("submitMonth", pos.get(0).getSubmitMonth());

                            progressReportDAO.saveReportProgress(mapAn);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rtnCode;
    }

    @Override
    public String getMaxReportdtosLimitOne(Map<String, String> map) {
        String po = progressReportDAO.getMaxReportdtosLimitOne(map);
        return po;
    }

    @Override
    public Integer updateByExampleSelective(ProgressReportDTO progressReportDto) {
        ProgressReportPO progressReportPO = new ProgressReportPO();
        progressReportPO.setTelephone(progressReportDto.getTelephone());
        //创建条件类
        AcionDetailRateExample acionDetailRateExample = new AcionDetailRateExample();

        //创建条件包
        AcionDetailRateExample.Criteria criteria = acionDetailRateExample.createCriteria();

        //往条件包加条件
        criteria.andActionIdEqualTo(progressReportDto.getActionId());
        criteria.andSpicFirstdeptIdEqualTo(progressReportDto.getSpicFirstDeptId());
        int a = progressReportDAO.updateByExampleSelective(progressReportPO, acionDetailRateExample);

        return a;
    }

    @Override
    public Integer countByExample(String def2, String def3) {
        Map<String, String> map = new HashMap<>();
        map.put("def2", def2);
        map.put("def3", def3);
        return progressReportDAO.countByExample(map);
    }

    @Override
    public Integer delFile(String id) {
        return progressReportDAO.delFile(id);
    }

    @Override
    public Integer countByExampleByIn(String def3, String[] arrays) {
        return progressReportDAO.countByExampleByIn(def3, arrays);
    }

    @Override
    public List<String> selectActionIdByLike(Object[] ids) {
        return progressReportDAO.selectActionIdByLike(ids);
    }

    @Override
    public YearAndMonthDTO getMaxYearAndMonth(String actionId) {
        YearAndMonthPO po = progressReportDAO.getMaxYearAndMonth(actionId);
        YearAndMonthDTO dto = new YearAndMonthDTO();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

    @Override
    public String getThirdSumActRate(Map<String, Object> map) {

        return progressReportDAO.getThirdSumActRate(map);
    }

    @Override
    public String getLastestNowMessage(String actionId) {
        return progressReportDAO.getLastestNowMessage(actionId);
    }


    //    private Long getDayNum(String day1,String day2) throws Exception {
////        String  a1 = new SimpleDateFormat("yyyy-MM-dd").format(day1);
////        String  b1 = new SimpleDateFormat("yyyy-MM-dd").format(day2);
//        Date    a11 = new SimpleDateFormat("yyyy-MM-dd").parse(day1);
//        Date    b11 = new SimpleDateFormat("yyyy-MM-dd").parse(day2);
//        long dayNum = (a11.getTime()-b11.getTime())/(24*60*60*1000);
//        return dayNum;
//    }
    //获取两个日期之间的月份
    public static List<String> getMonthBetween(String minDate, String maxDate) throws Exception {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }

    /**
     * 若包含二级单位,则返回false
     *
     * @param paramDept
     * @return
     */
    private boolean isHaveGroupDept(String[] paramDept) {
        boolean isHaveGroupDept = true; //默认全都是集团部门
        for (String dept : paramDept) {
            if ("org".equals(dept.split("-")[0])) {
                isHaveGroupDept = false;
                return isHaveGroupDept;
            }
        }
        return isHaveGroupDept;
    }
}

